<script setup lang="ts">
import TableView from '@/components/TableView.vue'
import {
  NButton,
  NDataTable,
  NForm,
  NFormItem,
  NPagination,
  useMessage,
  type DataTableColumns,
  NSpace,
  NInput,
  NSelect
} from 'naive-ui'
import { h, ref, reactive, watch, onDeactivated } from 'vue'
import router from '@/router'
import { useRoute } from 'vue-router'
import { sendRequest } from '@/utils/api'
import { useViewerStore } from '../Common/store'

const message = useMessage()
const route = useRoute()
const useStore = useViewerStore()

const rowKey: any = (e: any) => e.id || ''
const columns: DataTableColumns = [
  {
    title: '档案ID',
    key: 'archiveId',
    width: '10rem',
    align: useStore.isCenter
  },
  {
    title: '档案名称',
    key: 'archiveName',
    width: '10rem',
    align: useStore.isCenter,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '400rem'
        }
      }
    }
  },
  // {
  //   title: '是否涉密',
  //   key: 'isSecret',
  //   width: '10rem',
  //   align: useStore.isCenter
  // },
  {
    title: '使用用途',
    key: 'archiveUse',
    width: '10rem',
    align: useStore.isCenter,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '400rem'
        }
      }
    }
  },
  {
    title: '使用方法',
    key: 'useType',
    width: '10rem',
    align: useStore.isCenter
  },
  {
    title: '申请时间',
    key: 'createdTime',
    width: '10rem',
    align: useStore.isCenter
  },
  {
    title: '审批状态',
    key: 'approvalStatus',
    width: '10rem',
    align: useStore.isCenter
  },
  {
    title: '操作',
    key: 'action',
    width: '10rem',
    align: useStore.isCenter,
    fixed: 'right',
    render: (e: any) => [
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          onClick: () => handleDetail(e)
        },
        {
          default: () => '详情'
        }
      ),
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          onClick: () => handleAgainAdd(e)
        },
        {
          default: () => '再次发起'
        }
      )
    ]
  }
]

//筛选条件
const formValue = reactive({
  archiveId: null,
  archiveName: null,
  isSecret: null
})
const paginationReactive = reactive({
  page: 1,
  pageSize: 10,
  showSizePicker: true,
  pageSizes: [10, 20, 50, 100, 500],
  itemCount: 0,
  onChange: (page: number) => {
    paginationReactive.page = page
    queryTableData()
  },
  onUpdatePageSize: (pageSize: number) => {
    paginationReactive.pageSize = pageSize
    paginationReactive.page = 1
    queryTableData()
  }
})

const isSecretList = [
  {
    value: '涉密',
    label: '涉密'
  },
  {
    value: '非涉密',
    label: '非涉密'
  }
]

//新增
const handleAdd = () => {
  router.push({
    path: '/OA/File/FileAdd'
  })
}

//详情
const handleDetail = (val: any) => {
  router.push({
    path: '/OA/File/FileDetail',
    query: {
      id: val.id
    }
  })
}

//再次发起
const handleAgainAdd = (val: any) => {
  router.push({
    path: '/OA/File/FileEdit',
    query: {
      id: val.id,
      flag: 1
    }
  })
}

//表格数据
const tableLoading = ref(false)
const tableData: any = ref([])
const queryTableData = async (resetPage = false) => {
  if (resetPage) paginationReactive.page = 1
  tableLoading.value = true
  const [error, res] = await sendRequest({
    module: 'file',
    api: 'fileList',
    override: {
      params: {
        ...formValue,
        page: paginationReactive.page,
        limit: paginationReactive.pageSize
      }
    }
  })
  tableLoading.value = false
  if (error) return message.error(error.message)
  paginationReactive.itemCount = res.data?.totalCount
  const tmp: any = res.data
  tableData.value = tmp?.list
}

const keyUpDown = (e: any) => e.key == 'Enter' && (paginationReactive.page = 1,queryTableData(true))
onDeactivated(() => window.removeEventListener('keyup', keyUpDown, false))

watch(
  () => route.path,
  async () => {
    if (route.path == '/OA/File/FileList') {
      await queryTableData()
      window.addEventListener('keyup', keyUpDown, false)
    }
  },
  { deep: true, immediate: true }
)
</script>

<template>
  <TableView class="table">
    <template #form>
      <NSpace justify="space-between">
        <NForm :show-feedback="false" label-placement="left" inline>
          <NFormItem label="卷内档号:">
            <NInput clearable placeholder="请输入卷内档号" v-model:value="formValue.archiveId" />
          </NFormItem>
          <NFormItem label="档案名称:">
            <NInput clearable placeholder="请输入档案名称" v-model:value="formValue.archiveName" />
          </NFormItem>
          <!-- <NFormItem label="是否涉密:">
            <NSelect clearable placeholder="请选择" style="width: 200rem" :options="isSecretList"
              v-model:value="formValue.isSecret"  />
          </NFormItem> -->
          <NFormItem>
            <NButton type="primary" style="width: 78px; margin-right: 40rem" ghost @click="queryTableData(true)">
              查询
            </NButton>
          </NFormItem>
        </NForm>
        <NSpace align="end">
          <NButton type="primary" @click="handleAdd">
            档案借阅
          </NButton>
        </NSpace>
      </NSpace>
    </template>

    <template #table="props">
      <NDataTable v-bind="props" :bordered="false" :row-key="rowKey" :columns="columns" :data="tableData"
        :loading="tableLoading" />
    </template>

    <template #pagination>
      <NPagination :page-sizes="paginationReactive.pageSizes" :item-count="paginationReactive.itemCount"
        :page="paginationReactive.page" :page-size="paginationReactive.pageSize"
        @update:page="paginationReactive.onChange" @update:page-size="paginationReactive.onUpdatePageSize"
        show-size-picker show-quick-jumper :suffix="() => '页'">
        <template #prefix>
          <span style="font-size: 14px;"> 共{{ paginationReactive.itemCount }}条</span>
        </template>
      </NPagination>
    </template>
  </TableView>
</template>

<style scoped lang="scss">
.table {
  height: 100%;
  padding: 29px 30px 42px;
}
</style>
